<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
$sid = is_numeric($_POST['pid']) ? $_POST['pid'] : $sGET['i'];
if(!is_numeric($sid)) die($Warning[10]);
$params = array('ID' => $sid,'isReturn' => 1);
if(!$survey = $sdb->survey($params)) MsgBox('1',$Warning[11],'2'); 
if(is_numeric($_POST['pid'])){
	$SC -> cache -> clearAll(); 
	$CookieName = "SURVEY_{$survey['ID']}";
	if(is_numeric($_POST['survey_radio']) || !empty($_POST['survey_checkbox'])){ 
		if($survey['Status'] != 1) MsgBox('1',$Warning[28],'-1'); 
		if($_COOKIE[$CookieName] != 'SongCMS'){
			if(is_numeric($_POST['survey_radio'])){ 
				$survey['SurveyCount'][$_POST['survey_radio']] ++;
			} else { 
				foreach($survey['SurveyCount'] as $i => &$n){
					if(in_array($i,$_POST['survey_checkbox'])) $n ++;	
				}
			}
			$survey['SurveyCount'] = implode('|',$survey['SurveyCount']);
			$SQL = "UPDATE `{$dbprefix}survey` SET `SurveyCount` =  '{$survey['SurveyCount']}',`SurveyPeople` = (`SurveyPeople` + 1) WHERE `ID` = {$sid};";
			if($sdb -> ExecuteSQL($SQL)){
				@setcookie($CookieName,'SongCMS',time()+31536000);
				MsgBox('0','',"./?fsurvey/l{$Lang}/i{$sid}");
			} else {
				MsgBox('1',$Warning[18],'-1');
			}
		} else {
			MsgBox('1',$Warning[27],'-1'); 
		}
	} else {
		MsgBox('1',$Warning[29],'-1');	
	}
} 
if (is_numeric($sGET['i'])){
	$xml_data_file = "tmp/survey/ampie_data_{$params['ID']}.xml";
	if(!ProduceXML($xml_data_file,$survey['SurveyOptions'],$survey['SurveyCount'])){
		die($Warning[26]."<br />{$xml_data_file}");	
	}
	$survey['xml_data_file'] = $xml_data_file.'?'.mt_rand();  
	$survey['CommentURL'] = $Lang == 1 ? "./?fcomment/t3/i{$survey['ID']}" : "./?fcomment/l{$Lang}/t3/i{$survey['ID']}"; 
	$survey['CURL'] = $Lang == 1 ? "./?fcomment/t3/i{$survey['ID']}" : "./?fcomment/l{$Lang}/t3/i{$survey['ID']}"; 
	$survey['CommentTotal'] = $sdb -> CommentTotal(3,$survey['ID']); 
	$SC->assign('survey',$survey);
} else {
	MsgBox('1',$Warning[10],'2');	
}
function ProduceXML($xmlfile,$titlearr,$numberarr){
	if($xmlfile && is_array($titlearr) && is_array($numberarr)){
		$colorarr = array('#003300','#FF9900','#0099FF','#669966','#663300','#990000','#9966CC','#FF6633','#333300','#CC66CC','#99CC33','#33CCCC','#99CC00','#66CC00','#990000','#336699','#CC9900','#CC00CC','#CC00FF','#0099FF');
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<pie>\r\n";
		$maxnum = max($numberarr); 
		foreach($titlearr as $i => &$n){
			$n = htmlspecialchars($n,ENT_QUOTES);
			$xml .= $numberarr[$i]==$maxnum ? chr(9)."<slice title=\"{$n}\" color=\"{$colorarr[$i]}\" pull_out=\"true\">{$numberarr[$i]}</slice>\r\n" : chr(9)."<slice title=\"{$n}\" color=\"{$colorarr[$i]}\">{$numberarr[$i]}</slice>\r\n";
		}
		$xml .= "</pie>";
		return file_put_contents($xmlfile,$xml);
	} else {
		return false;	
	}
}
$SC -> register -> templateObject("sdb",$sdb);
include_once($CodeList . 'top.php');
include_once($CodeList . 'bottom.php');
$SC -> display($TemFile,$GetUrlPar);
?>